/*=============================================================================

  SONY CAM INTERFACE DEFINITION FOR INTERNAL USAGE

  FILE: hw_sony_cam_mod.h
  
  DESCRIPTION

	SONY CAM Interface Definition

  EXTERNALIZED FUNCTIONS

Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when          who       what, where, why
  ---------  -------  -------------------------------------------------------
    13/10/14   jyko       Initial version
   
=============================================================================*/

#ifndef _HW_SONY_CAM_MOD_H_
#define _HW_SONY_CAM_MOD_H_

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <OBase.h>
#include <mxType.h>
#include <mxBase/RS232.h>
#include <mxBase/MUtil.h>
#include <devSP/dev_mem.h>
#include "hw_sony_cam.h"

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/
#define CAM_TIME_UNIT			(10) // 100 usec. (in msec. mode)

//---------------------------------------------------------
// TX DEFIITION
//---------------------------------------------------------
#define TX_LEN					(128)
#define TX_LEN_MIN				(4)

#define TX_ETX					(0x0A) // LF

#define TX_ETX1					(0x0D) // CR
#define TX_ETX2					(TX_ETX)  // LF


//---------------------------------------------------------
// RX DEFIITION
//---------------------------------------------------------
#define RX_LEN					(4000*6)
#define RX_LEN_MIN				(6)

#define RX_ERX					(0x0D) // CR  ............. CAM manual bug?

#define RX_ERX1					(0x0A) // LF
#define RX_ERX2					(RX_ERX)  // CR

#define CAM_WAIT_TIME			(500)

//---------------------------------------------------------
//   SERIAL COMM.
//---------------------------------------------------------
typedef enum
{
	TX_NONE_S = 0x00,
	TX_IDLE_S = 0x01,
	TX_SYNC_S = 0x02,

} SCOM_TX_STATE;

typedef enum
{
	RX_NONE_S = 0x00,
	RX_IDLE_S,
	RX_RECV_S
} SCOM_RX_STATE;

typedef enum
{
	TX_REQ_T = 0x0001,
	TX_QUERY_T = 0x0002,	
	
	TX_INT1_VAL = 0x0010,
	TX_INT2_VAL = 0x0020,
	TX_INT3_VAL = 0x0040

} SCOM_TX_TYPE;

/*=============================================================================
  MACRO DEFINITION
=============================================================================*/
#define CAMMOD(a)			((cam_s*)a)
#define ISEMUL(a)			(((cam_s*)a)->emul)
#define CAMEMUL(a)			(((cam_s*)a)->em_cxt)

// Error Handling
#define CAMERR_SET(cam, errCode)				cam_err_set(cam, errCode)
#define CAMERR_CLEAR(cam)						cam_err_set(cam, CAM_NONE_EC)

/*=============================================================================
  STRUCTURE DEFINITION
=============================================================================*/
//---------------------------------------------------------
//   SERIAL COMM.
//---------------------------------------------------------
// TX & RX
typedef struct
{
	dword		type;
	
	// Nomaly ack.
	boolean		ack;

	// Integer
	int32		nval1;
	int32		nval2;
	int32		nval3;

	// Application
	//CAM_STS_s	app;
} scom_val_s;

typedef struct
{
	dword			state;
	char				*ptr;
	char				*tmp;
	int32			len;
	int32			len_trans;
	scom_val_s		val;
} tx_s;

typedef struct
{
	dword		state;
	char			*ptr;
	char			*mean; // Meaning string
	char			*tmp; // Temp.
	int32		len;
	int32		len_recv;
} rx_s;

//---------------------------------------------------------
//   DEVICE CONTEXT
//---------------------------------------------------------
typedef struct
{
	// Serial comm. interface
	mcxt			com;  // device context
	byte				com_port;
	byte				baud_rate;
	tx_s				*tx;  // Trans data handler
	rx_s				*rx;  // Receive data handler

	//--------------------------
	// Application Specific.
	//--------------------------
	// Configuration
	char				ver[64];
	CAM_CFGEX_s	cfg_ex;
	//first trig error
	//boolean         trig_first, sep_first;
	// Core structure
	mcxt			sync;  // sync. with serial comm.
	int32			wait_time;
	int32			err; // error code
	ObjClient_s		*cli;
	dword			devID;

	// Emulation
	boolean			emul;
	mcxt			em_cxt;
} cam_s;


/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/

#if defined(__cplusplus)
extern "C"
{
#endif

//---------------------------------------------------------
//   EVENT HANDLING
//---------------------------------------------------------
void cam_send_event(cam_s *cam, dword evt, dword wp, dword lp);

//---------------------------------------------------------
//   ERROR HANDLING
//---------------------------------------------------------
void cam_err_set(cam_s *cam, CAM_ERRORCODE errCode);
void cam_err_clear(cam_s *cam);

//---------------------------------------------------------
//   SERIAL COMM.
//---------------------------------------------------------
// RS-232 COMM.
boolean scom_tx_req(cam_s *cam);

// RECV.
void scom_rx_sync(cam_s *cam, char *ptr ,int32 len);
void scom_rx_async(cam_s *cam, char *ptr ,int32 len);

// Event
dword scom_event_cb(dword dwEvent, dword wParam, dword lParam, mcxt pUserCxt);
void scom_rx_trace(char *ptr, int32 len);

//---------------------------------------------------------
//   TX & RX HANDLER
//---------------------------------------------------------
// TX
tx_s *tx_alloc(int32 len);
void tx_free(tx_s *tx);
void tx_clear(tx_s *tx);
void tx_clear_val(tx_s *tx);
dword tx_state(tx_s *tx);
void tx_state_on(tx_s *tx, dword state);
void tx_state_off(tx_s *tx, dword state);
dword tx_type(tx_s *tx);
void tx_type_set(tx_s *tx, dword type);

// RX
rx_s *rx_alloc(int32 len);
void rx_free(rx_s *rx);
void rx_clear(rx_s *rx);
dword rx_state(rx_s *rx);
boolean rx_add(rx_s *rx, char val);
void rx_state_set(rx_s *rx, dword state);
void rx_print(rx_s *rx);




//---------------------------------------------------------
// EMULATION
//---------------------------------------------------------
boolean cam_InitializeEMUL(mcxt dev, byte comPort, byte baudRate /*CAM_BAUDRATE*/, int32 waitTime);
boolean cam_TerminateEMUL(mcxt dev);

// ADVANCED
boolean cam_CFGEXsetEMUL(mcxt dev, CAM_CFGEX_s *cfg);

// SAVE TO DEVICE
boolean cam_CFGSaveEMUL(mcxt dev);

// SETUP
boolean cam_GAINsetEMUL(mcxt dev, int32 gain);
boolean cam_SHUTTERsetEMUL(mcxt dev, int32 sht);
boolean cam_WBsetEMUL(mcxt dev, int32 mod);
boolean cam_RGAINsetEMUL(mcxt dev, int32 rgain);
boolean cam_BGAINsetEMUL(mcxt dev, int32 bgain);
boolean cam_WBWsetEMUL(mcxt dev, int32 enable);
boolean cam_SHARPsetEMUL(mcxt dev, int32 shp);
boolean cam_GAMsetEMUL(mcxt dev, int32 mod);
boolean cam_BINsetEMUL(mcxt dev, int32 val);
boolean cam_COLBARsetEMUL(mcxt dev, int32 enable);	// for XCL-U1000C only
boolean cam_BRTsetEMUL(mcxt dev, int32 brt);

//---------------------------------------------------------
// TRIGGER MODE
//---------------------------------------------------------
boolean cam_TRIGsetEMUL(mcxt dev, int32 mod/*CAM_TRIGGER*/);

#if defined(__cplusplus)
}
#endif

#endif // _HW_SONY_CAM_MOD_H_

/*=============================================================================
  EXTERNALIZED FUNCTION DESCRPTIONS
=============================================================================*/

/*--- END OF hw_sony_cam_mod.h ---*/
